package org.adit.spring.hibernate.service;

import java.util.List;

import org.adit.spring.hibernate.dao.PontoDao;
import org.adit.spring.hibernate.dao.RecursoDao;
import org.adit.spring.hibernate.dao.ResourceTypeDao;
import org.adit.spring.hibernate.dao.RouteDao;
import org.adit.spring.hibernate.entity.Ponto;
import org.adit.spring.hibernate.entity.Recurso;
import org.adit.spring.hibernate.entity.ResourceType;
import org.springframework.beans.factory.annotation.Autowired;

import br.com.caelum.vraptor.ioc.Component;

@Component
public class RouteService {
    @Autowired
	private RouteDao dao;    
    @Autowired
	private ResourceTypeDao resourceTypeDao;
    @Autowired
	private RecursoDao recursoDao;
    @Autowired
	private PontoDao pontoDao;
	
    
    //@ReadOnly
	public String setFooter() {
		return "</FleetEngineInput>";
	}
    //@ReadOnly
	public String setJobs(List<Ponto> list) {
		return "<Jobs>".concat(setJobContent(list)).concat("</Jobs>");	
		
	}
    //@ReadOnly
	public String setJobContent(List<Ponto> list) {
		String job = "";
		for (Ponto ponto : list) {
			job.concat("<Job Id="+ponto.getId()+">" +
					      "<Demands>" +
					        "<Demand Ind=\"1\">"+ponto.getDemand()+"</Demand>" +
					      "</Demands>" +
					      "<Pickup>" +
					        "<X>"+ponto.getX()+"</X>" +
					        "<Y>"+ponto.getY()+"</Y>" +
					      "</Pickup>" +
					    "</Job>");
		}
	        return job;
	}
    //@ReadOnly
	public String setResouces(List<Recurso> list) {
		return "<Resources>".concat(setResourceContent(list)).concat("</Resources>");	
	}
	
    //@ReadOnly
	public String setResourceContent(List<Recurso> list) {
		String rt = "";
		for (Recurso recurso : list) {
			rt.concat("<Resource Id="+recurso.getId()+">" +
		      "<ResourceType>"+recurso.getResourceTypeId()+"</ResourceType>" +
		      "<ResourcePeriods>" +
		        "<ResourcePeriod Id=\"RP1\">" +
		          "<StartDepot>D1</StartDepot>" +
		          "<StopDepot>D1</StopDepot>" +
		          "<Start>2010-01-01T08:00:00</Start>" +
		          "<Stop>2010-12-31T17:59:59</Stop>" +
		        "</ResourcePeriod>" +
		      "</ResourcePeriods>" +
		    "</Resource>");
		}
		return rt;
	}
    
    //@ReadOnly
	public String setResoucesType(List<ResourceType> list) {		
		return "<ResourceTypes>".concat(setResourceTypeContent(list)).concat("</ResourceTypes>");
	}

    //@ReadOnly
	public String setResourceTypeContent(List<ResourceType> list) {
		String rtc = "";
		for (ResourceType resourceType : list) {
			rtc.concat("<ResourceType Id=" + resourceType.getResourceType()+">" +
		      "<Cost>"+ resourceType.getCost() +"</Cost>" +
		      "<CostKM>"+ resourceType.getCostKM() + "</CostKM>" +
		      "<CostDriveTime>"+ resourceType.getCostDriveTime() + "</CostDriveTime>" +
		      "<CostWaitTime>"+ resourceType.getCostWaitTime() + "</CostWaitTime>" +
		      "<CostServiceTime>0</CostServiceTime>" +
		      "<Capacities>" +
		        "<Capacity Ind=\"1\">" + resourceType.getCapacity().getQuantidade() + "</Capacity>" +
		      "</Capacities>" +
		      "<Network>13</Network>" +
		    "</ResourceType>");
		}
		return rtc;
	}

    //@ReadOnly
	public String setHeader() {
		String header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<FleetEngineInput xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:noNamespaceSchemaLocation=\"input.xsd\">" 
				+ "<Parameters>"
				+ "<Matrix>StreetNetwork</Matrix>" 
				+ "<NumCapacities>1</NumCapacities>" 
				+ "<MaxCalculationTime>1</MaxCalculationTime>" 
				+ "<RandomSeed>1</RandomSeed>" 
				+ "</Parameters>"
				
				+ "<Depots>    <Depot Id=\"D1\">      <X>-41.09270</X>      <Y>-20.89175</Y>   </Depot>  </Depots>";
		
		dao.listaTudo();
		return header;
	}

}
